package com.example.hcd.functionactivity;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.hcd.MenuActivity;
import com.example.hcd.R;
import com.example.hcd.adapt.FileAdapter;
import com.example.hcd.datamanage.DepartmentData;
import com.example.hcd.datamanage.LoadedImage;
import com.example.hcd.datamanage.RoleInfoData;
import com.example.hcd.datamanage.UserInfoData;
import com.example.hcd.internet.FileUtils;
import com.example.hcd.internet.InternetKeep;
import com.example.hcd.internet.LinkHelp;
import com.example.hcd.internet.URLS;
import com.example.hcd.tool.MyGridView;
import com.example.hcd.tool.MyLoadingDialog;
import com.example.hcd.tool.ToastHelp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class NoticeNewCreate extends BaseActivity implements OnClickListener{

	private LayoutInflater inflater;
	private View main_view;
	private boolean isStick = false;
	//部门信息
	private ArrayList<DepartmentData> deparListInfo;
	//角色信息
	private ArrayList<RoleInfoData> roleInfoData;
	//用户信息
	private ArrayList<UserInfoData> userInfoData;
	
	//缓存信息
	//部门信息
	private ArrayList<DepartmentData> deparListInfoCache;
	//角色信息
	private ArrayList<RoleInfoData> roleInfoDataCache;
	//用户信息
	private ArrayList<UserInfoData> userInfoDataCache;
	
	
	//部门信息框
	private EditText depEdit;
	//用户信息框
	private EditText userEdit;
	//角色信息框
	private EditText roleEdit;
	
	//附件的GridView
	private MyGridView gridview;

	//附件的地址集合
	private ArrayList<String> listfile=new ArrayList<String>(); 
	private ArrayList<String> listfileCatch=new ArrayList<String>(); 
	
	private FileAdapter fileAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mPageName = "NoticeNewCreate";
		inflater = LayoutInflater.from(this);
		main_view = inflater.inflate(R.layout.notice_create_view, null);
		setContentView(main_view);
	
		initView();
	}
	
	private void initView()
	{
		// 返回键
		ImageView backBtn = (ImageView) findViewById(R.id.title_bar_btn_left);
		// 提交键
		ImageView uploadBtn = (ImageView) findViewById(R.id.title_bar_btn_right);
		backBtn.setOnClickListener(this);
		uploadBtn.setOnClickListener(this);
		
		//初始化信息
		{
		deparListInfo = InternetKeep.getInstence().getDepartmentInfolist();
		//角色信息
		roleInfoData = InternetKeep.getInstence().getRoleInfolist();
		//用户信息
		userInfoData =InternetKeep.getInstence().getUserinfolist();
		
		//缓存信息
		//部门信息
		deparListInfoCache = new ArrayList<DepartmentData>();
		//角色信息
		roleInfoDataCache = new ArrayList<RoleInfoData>();
		//用户信息
		userInfoDataCache = new ArrayList<UserInfoData>();
		
		depEdit = (EditText)main_view.findViewById(R.id.editText1);
		
		userEdit = (EditText)main_view.findViewById(R.id.editText2);
		
		roleEdit = (EditText)main_view.findViewById(R.id.editText3);
			
		}
		
		//添加部门
		ImageView addDep = (ImageView)main_view.findViewById(R.id.add1);
		addDep.setOnClickListener(this);
		
		//添加用户
		ImageView addUser = (ImageView)main_view.findViewById(R.id.add2);
		addUser.setOnClickListener(this);
		
		//添加角色
		ImageView addRole = (ImageView)main_view.findViewById(R.id.add3);
		addRole.setOnClickListener(this);
		
		//获取是否置顶
		final EditText stickEdit = (EditText)main_view.findViewById(R.id.edittext5);
		stickEdit.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				if(stickEdit.getText().toString().equals("")||stickEdit.getText().toString()==null)
				{
					stickEdit.setText("0");
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				
				
			}
		});
		CheckBox isStickBox = (CheckBox)main_view.findViewById(R.id.CheckBox51);
		isStickBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					stickEdit.setEnabled(true);
					stickEdit.setText("0");
					stickEdit.setFocusable(true);
					
				}else
				{
					stickEdit.setEnabled(false);
					stickEdit.setText("置顶天数");
				}
			}
		});
		gridview  = (MyGridView)main_view.findViewById(R.id.file_gridview);
		gridview.setVisibility(View.GONE);
		fileAdapter = new FileAdapter(this);  
		gridview.setAdapter(fileAdapter); 
		
		gridview.setOnItemLongClickListener(new OnItemLongClickListener() {
			 //GridList长按了某项
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3){
            //这里的arg2代表长按的位置
            fileAdapter.photos.remove(arg2);//移除数据
            listfile.remove(arg2);
            
            if(listfile.size()==0)
            {
            	gridview.setVisibility(View.GONE);
            }
            fileAdapter.notifyDataSetInvalidated();
            return true;
            }
		});
		
		gridview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				LoadedImage uri = fileAdapter.getItem(arg2);
				//System.out.println(uri.getText());
				if(FileUtils.isPic(getLast(uri.getPicPath())))
				{
					ArrayList<String> picpathlist = new ArrayList<String>(); 
					Intent intent = new Intent(NoticeNewCreate.this, ShowPictureList.class);
					for(int i = 0;i<listfile.size();i++)
					{
						if(FileUtils.isPic(listfile.get(i)))
						{
							picpathlist.add(listfile.get(i));
						}
					}
					
					for(int i = 0;i<picpathlist.size();i++)
					{
						if(picpathlist.get(i).equals(uri.getPicPath()))
						{
							intent.putExtra("MOREN_INDEX", i);
						}
					}
					
					
					intent.putStringArrayListExtra("PICK_PATH_LIST", picpathlist);
					
					intent.putExtra("IS_BENDI", true);
					startActivity(intent);
				}else
				{
					Intent intent = new Intent(NoticeNewCreate.this, FileDetailActivity.class);
					intent.putExtra("FILE_URI",uri.getPicPath());
					intent.putExtra("FILE_NAME", getLast(uri.getPicPath()));
					intent.putExtra("IS_BENDI", true);
					startActivity(intent);
				}
				
				overridePendingTransition(R.anim.out_alpha, R.anim.enter_alpha);
			}
		});
		
		ImageView appendix_btn = (ImageView)main_view.findViewById(R.id.appendix_btn);
		appendix_btn.setOnClickListener(this);
		
		//有效期
		//起始
		//结束
		
	}
	

	
	private String getLast(String path)
    {
    	String a[] = path.split("/");
    	return a[a.length-1];
    }

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
		// //隐藏软键盘
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(main_view.getWindowToken(), 0);

		getCurrentFocus().clearFocus();
		EditText edit = (EditText)main_view.findViewById(R.id.edit_focuse_unable);
		edit.setFocusable(true);
		edit.requestFocus();
		
		
		int id = arg0.getId();
		switch (id) {
		case R.id.title_bar_btn_left:
			finish();
			break;
			
		case R.id.title_bar_btn_right:
			upload();
			break;
			
		case R.id.add1:
			showDepAlert();
			break;
			
		case R.id.add2:

			showUserAlert();
			break;
			
		case R.id.add3:
			showRoleAlert();

			break;
			
		case R.id.appendix_btn:
			Intent intent = new Intent(NoticeNewCreate.this,SelectPicActivity.class);
			intent.putExtra("type", "choosemore");
			intent.putExtra("IS_CHOOSE_FILE", true);
			startActivityForResult(intent, MenuActivity.SELECT_PHOTO);
			overridePendingTransition(R.anim.out_alpha, R.anim.enter_alpha);
			break;

		default:
			break;
		}
		
	}
	
	@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        	if(resultCode == Activity.RESULT_OK)
        	{
        		switch (requestCode) {
        		   
                case MenuActivity.SELECT_PHOTO:
            	//ArrayList<String> files =new ArrayList<String>();
                listfileCatch.clear();
            	listfileCatch = data.getStringArrayListExtra("files");
            	listfile.addAll(listfileCatch);
            	initGridView();
                break;

                  
                default:
                    break;
                }
        	}
        
            
    }
	
	private void initGridView()
	{
		new AsyncLoadedImage().execute();
			
	}
	
	/* 
     * 异步加载缩略图到LoadedImage然后调用addImage方法更新Adapter 
     */  
    class AsyncLoadedImage extends AsyncTask<Object, LoadedImage, Object> {  
        @Override  
        protected Object doInBackground(Object... params) {  
            
            Bitmap bitmap = null;  
            Bitmap newBitmap;  
            for (int i = 0; i < listfileCatch.size(); i++) {  
                String path =listfileCatch.get(i);  
                
                File file = new File(path);
            	if(file.exists())
            	{
            		System.out.println(path+"存在");
            		if(FileUtils.isPic(path))
                    {
                    	
                    	try {  
                            BitmapFactory.Options options = new BitmapFactory.Options();  
                            options.inSampleSize = 10;  
                            bitmap = BitmapFactory.decodeFile(path, options);  
                            newBitmap = ThumbnailUtils.extractThumbnail(bitmap,  
                                    120, 120);  
                            bitmap.recycle();  
                            if (newBitmap != null) {  
                                publishProgress(new LoadedImage(newBitmap,path ));  
                            }  
                        } catch (Exception e) {  
                            e.printStackTrace();  
                        } 
                    	
                    }else
                    {
                		try { 
                			
                			if(path.contains(".pdf"))
                        	{
                    			bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pdf);
                        	}else if(path.contains(".txt"))
                        	{
                        		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.text);
                        	}else if(path.contains(".doc"))
                        	{
                        		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.word);
                        	}else if(path.contains(".zip"))
                        	{
                        		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zip);
                        	}else if(path.contains(".xls"))
                        	{
                        		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.excel);
                        	}else
                        	{
                        		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.file);
                        	}
                            newBitmap = ThumbnailUtils.extractThumbnail(bitmap,  
                                    120, 120);  
                            bitmap.recycle();  
                            if (newBitmap != null) {  
                                publishProgress(new LoadedImage(newBitmap,path ));  
                            }  
                        } catch (Exception e) {  
                            e.printStackTrace();  
                        } 

                    }
            	}else
            	{
            		listfile.remove(path);
            	}
                
                 
            }  
       
        return null;  
    }  
  
        //实时更新UI  onProgressUpdate()方法的参数对应于doInBackground中publishProgress方法的参数，同时也对应于  
        //doInBackground的第二个参数  
        @Override  
        public void onProgressUpdate(LoadedImage... value) {  
            addImage(value);  
        }
        
        
  
        //更新UI结束后的处理  
        @Override  
        protected void onPostExecute(Object result) {  
            setProgressBarIndeterminateVisibility(false);  
        }  
    } 
    
    /* 
     * 刷新Adapter 
     */  
    	private void addImage(LoadedImage... value) {  
        for (LoadedImage image : value) {  
            fileAdapter.addPhoto(image);  
            fileAdapter.notifyDataSetChanged();  
            gridview.setVisibility(View.VISIBLE);
        }  
    } 
	
	private void showDepAlert()
	{
		final AlertDialog dep = new AlertDialog.Builder(NoticeNewCreate.this).create();
		dep.show();
		//View alertView = inflater.inflate(R.layout.choose_more_view, null);
		dep.getWindow().setContentView(R.layout.choose_more_view);
		
		TextView title = (TextView)dep.getWindow().findViewById(R.id.title);
		title.setText("请选择部门");
		LinearLayout chooseItem = (LinearLayout)dep.getWindow().findViewById(R.id.addline);
		
		for(int i=0;i<deparListInfo.size();i++)
		{
			final DepartmentData depa = deparListInfo.get(i);
			
			View itemView = inflater.inflate(R.layout.choose_more_item, null);
			CheckBox check = (CheckBox)itemView.findViewById(R.id.CheckBox55);
			check.setText(deparListInfo.get(i).name);
			
			if(deparListInfoCache.contains(depa))
			{
				check.setChecked(true);
			}
			
			check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
					// TODO Auto-generated method stub
					if(arg1)
					{
						if(!deparListInfoCache.contains(depa))
						{
							deparListInfoCache.add(depa);
						}
					}else
					{
						if(deparListInfoCache.contains(depa))
						{
							deparListInfoCache.remove(depa);
						}
					}
				}
			});
			
			chooseItem.addView(itemView);
		}
		
		dep.setCancelable(false);
		
		TextView cancelBtn = (TextView)dep.getWindow().findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				deparListInfoCache.clear();
				dep.dismiss();
			}
		});
		TextView suerBtn = (TextView)dep.getWindow().findViewById(R.id.sure_btn);
		suerBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(deparListInfoCache.size()>0)
				{
					String info="";
					for(int i=0;i<deparListInfoCache.size();i++)
					{
						if(i==0)
						{
							info = deparListInfoCache.get(i).name;
						}else
						{
							info+=","+deparListInfoCache.get(i).name;
						}
						
					}
					depEdit.setText(info);
				}
				
				
				dep.dismiss();
			}
		});
		

	}
	
	private void showRoleAlert()
	{
		final AlertDialog roleDia = new AlertDialog.Builder(NoticeNewCreate.this).create();
		roleDia.show();
		//View alertView = inflater.inflate(R.layout.choose_more_view, null);
		roleDia.getWindow().setContentView(R.layout.choose_more_view);
		
		TextView title = (TextView)roleDia.getWindow().findViewById(R.id.title);
		title.setText("请选择角色");
		LinearLayout chooseItem = (LinearLayout)roleDia.getWindow().findViewById(R.id.addline);
		
		for(int i=0;i<roleInfoData.size();i++)
		{
			final RoleInfoData role = roleInfoData.get(i);
			
			View itemView = inflater.inflate(R.layout.choose_more_item, null);
			CheckBox check = (CheckBox)itemView.findViewById(R.id.CheckBox55);
			check.setText(roleInfoData.get(i).getRoleName());
			
			if(roleInfoDataCache.contains(role))
			{
				check.setChecked(true);
			}
			
			check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
					// TODO Auto-generated method stub
					if(arg1)
					{
						if(!roleInfoDataCache.contains(role))
						{
							roleInfoDataCache.add(role);
						}
					}else
					{
						if(roleInfoDataCache.contains(role))
						{
							roleInfoDataCache.remove(role);
						}
					}
				}
			});
			
			chooseItem.addView(itemView);
		}
		
		roleDia.setCancelable(false);
		
		TextView cancelBtn = (TextView)roleDia.getWindow().findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				roleInfoDataCache.clear();
				roleDia.dismiss();
			}
		});
		TextView suerBtn = (TextView)roleDia.getWindow().findViewById(R.id.sure_btn);
		suerBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(roleInfoDataCache.size()>0)
				{
					String info="";
					for(int i=0;i<roleInfoDataCache.size();i++)
					{
						if(i==0)
						{
							info = roleInfoDataCache.get(i).getRoleName();
						}else
						{
							info+=","+roleInfoDataCache.get(i).getRoleName();
						}
						
					}
					roleEdit.setText(info);
				}
				
				
				roleDia.dismiss();
			}
		});
		
	}
	
	private void showUserAlert()
	{
		final AlertDialog userDia = new AlertDialog.Builder(NoticeNewCreate.this).create();
		userDia.show();
		//View alertView = inflater.inflate(R.layout.choose_more_view, null);
		userDia.getWindow().setContentView(R.layout.choose_more_view);
		
		TextView title = (TextView)userDia.getWindow().findViewById(R.id.title);
		title.setText("请选择用户");
		LinearLayout chooseItem = (LinearLayout)userDia.getWindow().findViewById(R.id.addline);
		
		for(int i=0;i<userInfoData.size();i++)
		{
			final UserInfoData user = userInfoData.get(i);
			
			View itemView = inflater.inflate(R.layout.choose_more_item, null);
			CheckBox check = (CheckBox)itemView.findViewById(R.id.CheckBox55);
			check.setText(userInfoData.get(i).name);
			
			if(userInfoDataCache.contains(user))
			{
				check.setChecked(true);
			}
			
			check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
					// TODO Auto-generated method stub
					if(arg1)
					{
						if(!userInfoDataCache.contains(user))
						{
							userInfoDataCache.add(user);
						}
					}else
					{
						if(userInfoDataCache.contains(user))
						{
							userInfoDataCache.remove(user);
						}
					}
				}
			});
			
			chooseItem.addView(itemView);
		}
		
		userDia.setCancelable(false);
		
		TextView cancelBtn = (TextView)userDia.getWindow().findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				userInfoDataCache.clear();
				userDia.dismiss();
			}
		});
		TextView suerBtn = (TextView)userDia.getWindow().findViewById(R.id.sure_btn);
		suerBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(userInfoDataCache.size()>0)
				{
					String info="";
					for(int i=0;i<userInfoDataCache.size();i++)
					{
						if(i==0)
						{
							info = userInfoDataCache.get(i).name;
						}else
						{
							info+=","+userInfoDataCache.get(i).name;
						}
						
					}
					userEdit.setText(info);
				}
				
				
				userDia.dismiss();
			}
		});
		
	}
	
	private void upload()
	{
		if(iscanUpload())
		{
			UpdateLoadInfo load = new UpdateLoadInfo();
			load.execute();
		}
	}
	
	private boolean iscanUpload()
	{
		EditText edit = (EditText)main_view.findViewById(R.id.editText);
		if(edit.getText().toString().equals("")||edit.getText().toString()==null)
		{
			//Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
			ToastHelp.doToastInTop(this, "标题不能为空");
			return false;
		}
		
		edit = (EditText)main_view.findViewById(R.id.editText6);
		if(edit.getText().toString().equals("")||edit.getText().toString()==null)
		{
			//Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
			ToastHelp.doToastInTop(this, "内容不能为空");
			return false;
		}
		
		if((depEdit.getText().toString().equals("")||depEdit.getText().toString()==null)
				&&(userEdit.getText().toString().equals("")||userEdit.getText().toString()==null)
				&&(roleEdit.getText().toString().equals("")||roleEdit.getText().toString()==null))
		{
			
			//Toast.makeText(this, "发布对象不能全部为空", Toast.LENGTH_SHORT).show();
			ToastHelp.doToastInTop(this, "发布对象不能全部为空");
			return false;
		}
		
		
		return true;
	}
	
	/*
	 * 传送数据到服务器
	 */
	private class UpdateLoadInfo extends AsyncTask<String, Integer, String> {
		private MyLoadingDialog mProgress;
		public UpdateLoadInfo() {

			
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgress = new MyLoadingDialog(NoticeNewCreate.this, null, "上传中...", true);
		}

		@Override
		protected String doInBackground(String... params) {
			String result = null;
			HttpResponse response = null;
			BufferedReader reader = null;
			try {
				
				MultipartEntity multipartEntity = new MultipartEntity();
		        for(String fileuri:listfile)
		        {
		        	File file = new File(fileuri);
		        	FileBody filebody = new FileBody(file, file.getName(), "application/octet-stream","UTF-8");
		        	multipartEntity.addPart(file.getName(), filebody);
		        }
		        EditText edit = (EditText)main_view.findViewById(R.id.editText);
		        multipartEntity.addPart("title",  new StringBody(edit.getText().toString(),Charset.forName("UTF-8")));
		        edit = (EditText)main_view.findViewById(R.id.editText6);
		        multipartEntity.addPart("content",  new StringBody(edit.getText().toString(),Charset.forName("UTF-8")));
				

				if(deparListInfoCache.size()>0)
				{
					String depId="";
					for(int i =0;i<deparListInfoCache.size();i++)
					{
						if(i==0)
						{
							depId=deparListInfoCache.get(i).id;
						}else
						{
							depId+=","+deparListInfoCache.get(i).id;
						}
						
					}
					System.out.println("depId="+depId);
					multipartEntity.addPart("depNames",  new StringBody(depId,Charset.forName("UTF-8")));
				}
				
				if(userInfoDataCache.size()>0)
				{
					String depId="";
					for(int i =0;i<userInfoDataCache.size();i++)
					{
						if(i==0)
						{
							depId=userInfoDataCache.get(i).id;
						}else
						{
							depId+=","+userInfoDataCache.get(i).id;
						}
						
					}
					System.out.println("users="+depId);
					multipartEntity.addPart("userNames",  new StringBody(depId,Charset.forName("UTF-8")));
				}
				
				if(roleInfoDataCache.size()>0)
				{
					String depId="";
					for(int i =0;i<roleInfoDataCache.size();i++)
					{
						if(i==0)
						{
							depId=roleInfoDataCache.get(i).getId();
						}else
						{
							depId+=","+roleInfoDataCache.get(i).getId();
						}
						
					}
					System.out.println("roles="+depId);
					multipartEntity.addPart("userRole",  new StringBody(depId,Charset.forName("UTF-8")));
				}
				
				CheckBox isStickBox = (CheckBox)main_view.findViewById(R.id.CheckBox51);
				
				if(isStickBox.isChecked())
				{ multipartEntity.addPart("is_sticky_post",  new StringBody("1",Charset.forName("UTF-8")));
					 
					EditText stickEdit = (EditText)main_view.findViewById(R.id.edittext5);
					multipartEntity.addPart("sticky_post_days",  new StringBody(stickEdit.getText().toString(),Charset.forName("UTF-8")));
				}else
				{
					multipartEntity.addPart("is_sticky_post",  new StringBody("0",Charset.forName("UTF-8")));
				}
				
				response = LinkHelp.UpPhotos(URLS.getCreateNotice(), multipartEntity,false);

				if (response != null) {
					StatusLine line = response.getStatusLine();
					if (line.getStatusCode() == 200) {
						reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
						StringBuilder sb = new StringBuilder();
						for (String s = reader.readLine(); s != null; s = reader.readLine()) {
							sb.append(s);
						}
						reader.close();
						result = sb.toString();
						
						System.out.println(result);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			mProgress.dimiss();
			JSONObject jsonObject;
			if(result!=null)
			{
				try {
	                jsonObject = new JSONObject(result);
	                if (jsonObject.isNull("code")) {
	                    String status = jsonObject.getString("status");
	                    if (status.equals("success")) {
	                        Toast.makeText(NoticeNewCreate.this, "提交成功", Toast.LENGTH_SHORT).show();

	                        	finish();
	                    } else {
	                    	String msg = jsonObject.getString("msg");
	                    	Toast.makeText(NoticeNewCreate.this, msg, Toast.LENGTH_SHORT).show();
	                    	ToastHelp.doToastInTop(NoticeNewCreate.this, msg);
	                    }
	                    
	                }}  
	            catch (JSONException e) {
	                e.printStackTrace();
	            }
			}else
			{
				ToastHelp.doToastInTop(NoticeNewCreate.this, "网络异常");
			}
            
		}

	}
		
	


}
